package com.wan37.logic.tradingshop.dao.impl;

import com.wan37.logic.tradingshop.dao.TradingShopRecordDao;
import com.wan37.logic.tradingshop.domain.entity.TradingShopRecordDO;
import com.wan37.logic.tradingshop.domain.vo.TradingShopVO;
import com.wan37.logic.tradingshop.service.TradingShopRecordService;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-05 11:54
 **/
@Repository
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class TradingShopRecordDaoImpl implements TradingShopRecordDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;
    @Override
    public List<TradingShopRecordDO> findByTradingShopId(String tradingShopId) {
        return (List<TradingShopRecordDO>) hibernateTemplate.find("from TradingShopRecordDO where tradingShopId=?", tradingShopId);
    }

    @Override
    public List<TradingShopRecordDO> findByUserId(String userId) {
        return (List<TradingShopRecordDO>) hibernateTemplate.find("from TradingShopRecordDO where userId=?", userId);
    }

    @Override
    public List<TradingShopRecordDO> findByBuyId(String buyId) {
        return (List<TradingShopRecordDO>) hibernateTemplate.find("from TradingShopRecordDO where buyId=?", buyId);
    }

    @Override
    public List<TradingShopRecordDO> findByRecordId(String recordId) {
        return (List<TradingShopRecordDO>) hibernateTemplate.find("from TradingShopRecordDO where recordId = ?", recordId);
    }

    @Override
    public boolean deleteByTradingShopId(String tradingShopId) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from TradingShopRecordDO where tradingShopId = ?");
        query.setParameter(0, tradingShopId);
        query.executeUpdate();
        return false;
    }

    @Override
    public boolean add(TradingShopRecordDO tradingShopRecordDO) {
        hibernateTemplate.save(tradingShopRecordDO);
        return false;
    }

    @Override
    public boolean delete(Integer id) {
        return false;
    }

    @Override
    public boolean update(TradingShopRecordDO tradingShopRecordDO) {
        return false;
    }

    @Override
    public List<TradingShopRecordDO> getAll() {
        return (List<TradingShopRecordDO>) hibernateTemplate.find("from TradingShopRecordDO ");
    }
}
